﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Best.SmartCms.Utility
{
    /// <summary>
    ///  Md5加密
    /// </summary>
    public static class Md5Utility
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string Get32Md5One(string source)
        {
            using (MD5 md5Hash = MD5.Create())
            {
                byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(source));
                StringBuilder sBuilder = new StringBuilder();
                for (int i = 0; i < data.Length; i++)
                {
                    sBuilder.Append(data[i].ToString("x2"));
                }

                string hash = sBuilder.ToString();
                return hash.ToUpper();
            }
        }

        /// <summary>
        /// 获取16位md5加密
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string Get16Md5One(string source)
        {
            using (MD5 md5Hash = MD5.Create())
            {
                byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(source));
                //转换成字符串，并取9到25位
                string sBuilder = BitConverter.ToString(data, 4, 8);
                //BitConverter转换出来的字符串会在每个字符中间产生一个分隔符，需要去除掉
                sBuilder = sBuilder.Replace("-", "");
                return sBuilder.ToUpper();
            }
        }

        /// <summary>
        /// 不可逆加密
        /// </summary>
        /// <returns></returns>
        public static bool EncryptVerify(string source, string secret,  string pwd)
        {
            var pwd2 = Encrypt(pwd, secret);
            return pwd2 == source;
        }


        /// <summary>
        /// 用户密码加密
        /// </summary>
        /// <returns></returns>
        public static string Encrypt(string pwd, string secret)
        {
            byte[] hash = EncryptHmac(pwd, secret);
            return Byte2Hex(hash);
        }

        /// <summary>
        /// 密钥的私钥 (secret) 作为key，生成被签名串的 HmacMD5签名
        /// </summary>
        /// <param name="data"></param>
        /// <param name="secret"></param>
        /// <returns></returns>
        public static byte[] EncryptHmac(String data, String secret)
        {
            HMACMD5 hmacMd5 = new HMACMD5(Encoding.UTF8.GetBytes(secret));
            byte[] textBytes = Encoding.UTF8.GetBytes(data);
            byte[] encodedTextBytes = hmacMd5.ComputeHash(textBytes);
            return encodedTextBytes;
        }

        /// <summary>
        /// 将签名得到的16字节依次转化为大写的16进制字符串，如果字符串长度为1，在前补0，结果为32位字符串
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static string Byte2Hex(byte[] bytes)
        {
            StringBuilder sign = new StringBuilder();
            for (int i = 0; i < bytes.Length; i++)
            {
                var str = (bytes[i] & 0xFF).ToString("X2");
                String hex = str;
                if (hex.Length == 1)
                {
                    sign.Append("0");
                }

                sign.Append(hex.ToUpper());
            }

            return sign.ToString();
        }
    }
}
